package com.devicemantra.coordinate.operations;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.devicemantra.coordinate.dataobjects.CStateFactory;
import com.devicemantra.coordinate.exceptions.dataobjects.CDatabaseException;
import com.devicemantra.coordinate.exceptions.jaxb.CJAXBException;
import com.devicemantra.coordinate.exceptions.operations.CException;
import com.devicemantra.coordinate.exceptions.operations.CFileParameterNotFoundException;
import com.devicemantra.coordinate.exceptions.operations.CIOException;
import com.devicemantra.coordinate.exceptions.operations.CParameterNotFoundException;
import com.devicemantra.coordinate.exceptions.operations.CReadPayloadException;
import com.devicemantra.coordinate.exceptions.operations.CStateAlreadyExistsException;
import com.devicemantra.coordinate.interfaces.http.CHttpReader;
import com.devicemantra.coordinate.interfaces.http.CMultipartFormReader;
import com.devicemantra.coordinate.jaxb.*;
import com.oreilly.servlet.multipart.FilePart;

public class CRequestBuilder 
{
	private Request mRequest = null;
	private ObjectFactory mJAXBObjectFactory = null;
	private HttpServletRequest mHTTPServletReq;
	private String mXmlInputStr = null;
	private CMultipartFormReader mMultipartFormReader = null;
	
	public CRequestBuilder(HttpServletRequest pHTTPReq)
	{
		mJAXBObjectFactory = new ObjectFactory();
		mHTTPServletReq = pHTTPReq;
		System.out.println("Request content type is " + pHTTPReq.getContentType());
	}
	
		
	@SuppressWarnings("unused")
	public void processEvent() throws  CReadPayloadException, CJAXBException 
	{
		/* First read the incoming request */
		/* Read the Content of the HTTP request as a String representation */
		mXmlInputStr = CHttpReader.getAsString(mHTTPServletReq);
		mRequest = JAXBRequestFactory.parseRequest(mXmlInputStr);
		
	}
	
	@SuppressWarnings("unused")
	public void processFormFileEvent() throws CJAXBException, CParameterNotFoundException, CException, CIOException  
	{
		/* First read the incoming request */
		/* Read the Content of the HTTP form request  */
		mMultipartFormReader = new CMultipartFormReader(mHTTPServletReq);
		mMultipartFormReader.processForm();
		/* We have all the parameters parsed into the CMultipartFormReader object now */
		/* Get the XML Input and parse it */
		mXmlInputStr = mMultipartFormReader.getParameter("XML_PARAM");
		System.out.println("Got XML_PARAM = " + mXmlInputStr);
		mRequest = JAXBRequestFactory.parseRequest(mXmlInputStr);
	}
	
	public OperationType getOperationType()
	{
		OperationType lOperationType = mRequest.getCOperationType();
		System.out.println("Operation tpye " + lOperationType);
		return lOperationType;
	}
	
	
	public boolean isOperationPermitted(OperationType pOperationType) throws CException
	{
		OperationType lOpType = getOperationType();
		if(lOpType != pOperationType) {
			/** 409 Conflict - Indicates that the request could not be processed because of conflict in the request 
			 *  Requested operation is something different from the url hit */
			throw new CException("ERROR: Mismatch RequestData not matching Operation" + ": Request was directed at  " + pOperationType + " Operation data showed " + lOpType);
		}
		return false;
	}
	
	public Request getRequestObj()
	{
		return mRequest;
	}
	
	public FilePart getFile(String pName) throws CFileParameterNotFoundException
	{
		return mMultipartFormReader.getFile(pName);
	}
	
	public byte[] getFileAsBytes(String pName) throws CFileParameterNotFoundException
	{
		return mMultipartFormReader.getFileAsBytes(pName);
	}
	
	
	public String toString()
	{
		return mXmlInputStr;
	}
	
	
}
